package com.ruiyi.adrobot.beans;

import io.netty.channel.ChannelHandlerContext;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NettyChannelhandlerContextMap {
    private static Map<String, ChannelHandlerContext> map = new ConcurrentHashMap<String, ChannelHandlerContext>();

    public static void add(String simCode, ChannelHandlerContext ChannelHandlerContext) {
        map.put(simCode, ChannelHandlerContext);
    }

    public static ChannelHandlerContext get(String simCode) {
        return map.get(simCode);
    }

    public static String remove(ChannelHandlerContext channelHandlerContext) {
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == channelHandlerContext) {
                map.remove(entry.getKey());
                return entry.getKey().toString();
            }
        }
        return null;
    }

    public static int getSize() {
        return map.size();
    }

    public static List<ChannelHandlerContext> getChannelHandlerContextList() {
        List<ChannelHandlerContext> list = new LinkedList<>();
        for (Map.Entry entry : map.entrySet()) {
            list.add((ChannelHandlerContext) entry.getValue());
        }
        return list;
    }

    public static String getKeyByVal(ChannelHandlerContext ctx) {
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == ctx) {
                return entry.getKey().toString();
            }
        }
        return null;
    }

    public static List<String> getKeyList() {
        List<String> list = new LinkedList<>();
        for (Map.Entry entry : map.entrySet()) {
            list.add(entry.getKey().toString());
        }
        return list;
    }
}
